﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Net;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace QqForm
{
    /// <summary>
    /// QQ帮助操作类
    /// </summary>
    public class QqCommand
    {
        /// <summary>
        /// 登陆指令 1.4
        /// </summary>
        private string loginCmd1_4 = "VER=1.4&CON=1&CMD=Login&SEQ={0}&UIN={1}&PS={2}&M5=1&LG=0&LC=812822641C978097&GD=TW00QOJ9KUVD753S&CKE=\n";
        /// <summary>
        /// 获取好友列表
        /// </summary>
        private string getQqListCmd1_4 = "VER=1.4&CON=1&CMD=SimpleInfo2&SEQ={0}&UIN={1}&SID=&XP=C4CA4238A0B92382&UN={2}&TO=0\n";
        /// <summary>
        /// 获取在线好友列表
        /// </summary>
        private string getQqListOnlineCmd1_4 = "VER=1.4&CON=1&CMD=Query_Stat2&SEQ={0}&UIN={1}&SID=&XP=C4CA4238A0B92382&UN={2}&TO=0\n";
        /// <summary>
        /// 发送消息
        /// </summary>
        private string sendMsgCmd1_4 = "VER=1.4&CON=1&CMD=CLTMSG&SEQ={0}&UIN={1}&SID=&XP=C4CA4238A0B92382&UN={2}&MG={3}\n";

        private string addToListCmd1_4 = "VER=1.4&CON=1&CMD=AddToList&SEQ={0}&UIN={1}&SID=&XP=C4CA4238A0B92382&UN={2}\n";

        #region QQ登陆指令
        /// <summary>
        /// QQ登陆指令
        /// </summary>
        /// <param name="tcpClass"></param>
        public void LoginSendCommand(TcpClass tcpClass)
        {
            string strCmd = string.Format(loginCmd1_4, GetThreeRandom(), QqUserHelper.QqNum, QqUserHelper.QqPwd);
            tcpClass.Send(strCmd);
        }
        #endregion

        #region 获取QQ好友列表指令
        /// <summary>
        /// 获取QQ好友列表指令
        /// </summary>
        /// <param name="tcpClass"></param>
        /// <param name="un"></param>
        public void GetQqListSendCommand(TcpClass tcpClass, string un)
        {
            string strCmd = string.Format(getQqListCmd1_4, GetThreeRandom(), QqUserHelper.QqNum, un);
            byte[] bytes = Encoding.UTF8.GetBytes(strCmd);
            tcpClass.Send(bytes);
        }

        #endregion

        #region 送获取在线好友列表请求
        /// <summary>
        /// 发送获取在线好友列表请求
        /// </summary>
        public void GetQqListIsOnlineCommand(TcpClass tcpClass, string un)
        {
            string str = string.Format(getQqListOnlineCmd1_4, GetThreeRandom(), QqUserHelper.QqNum, un);
            byte[] date = Encoding.UTF8.GetBytes(str);
            tcpClass.Send(date);
        }
        #endregion

        #region 发送消息
        /// <summary>
        /// 发送消息
        /// </summary>
        /// <param name="tcpClass"></param>
        /// <param name="uid"></param>
        /// <param name="msg"></param>
        public void SendMsgCommand(TcpClass tcpClass, string uid, string msg)
        {
            string str = string.Format(sendMsgCmd1_4, GetThreeRandom(), QqUserHelper.QqNum, uid, msg);
            tcpClass.Send(str);
        }

        #endregion

        #region 添加QQ好友
        /// <summary>
        /// 添加QQ好友
        /// </summary>
        /// <param name="tcpClass"></param>
        /// <param name="un"></param>
        public void AddToListCommand(TcpClass tcpClass, string un)
        {
            string str = string.Format(addToListCmd1_4, GetThreeRandom(), QqUserHelper.QqNum, un);
            byte[] date = Encoding.UTF8.GetBytes(str);
            tcpClass.Send(date);
        }
        #endregion












        #region 登陆
        /// <summary>
        /// 登陆
        /// </summary>
        /// <param name="qqNum">QQ号码</param>
        /// <param name="pwd">QQ密码</param>
        /// <returns></returns>
        public PostQqServerBackResult QqLogin(string qqNum, string pwd)
        {
            string loginPostValue = string.Format(loginCmd1_4, GetThreeRandom(), qqNum, MD5Helper.ToMD5(pwd).ToUpper());
            byte[] byteArrarySendMsg = Encoding.UTF8.GetBytes(loginPostValue);
            byte[] byteResult = SendData(byteArrarySendMsg);
            string resultStr = Encoding.UTF8.GetString(byteResult);
            PostQqServerBackResult backResult = LoginResult(resultStr);
            return backResult;
        }


        #endregion

        #region Md5加密
        /// <summary>
        /// Md5加密
        /// </summary>
        /// <param name="toCryString"></param>
        /// <returns></returns>
        public static string Md5(string toCryString)
        {
            MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
            return BitConverter.ToString(hashmd5.ComputeHash(Encoding.Default.GetBytes(toCryString))).Replace("-", "").ToLower();//asp是小写,把所有字符变小写
        }

        #endregion

        #region 向QQ服务器发送请求指令
        /// <summary>
        /// 向QQ服务器发送请求指令
        /// </summary>
        /// <param name="byteArray">发送的数据集合</param>
        /// <returns></returns>
        private byte[] SendData(byte[] byteArray)
        {
            byte[] pageData = null;
            try
            {
                //pageData = new WebClient().UploadData("http://tqq.tencent.com:8000", "POST", byteArray);
                pageData = new WebClientPlus().UploadData("http://211.136.236.88:14000", "POST", byteArray);
            }
            catch (Exception ex)
            {
                pageData = Encoding.UTF8.GetBytes(ex.Message);
            }
            return pageData;
        }
        #endregion

        #region 登陆返回信息

        /// <summary>
        /// 登陆返回信息
        /// </summary>
        /// <param name="qqServerBackResult">QQ服务器返回的信息</param>
        /// <returns></returns>
        private PostQqServerBackResult LoginResult(string qqServerBackResult)
        {
            PostQqServerBackResult backResult = new PostQqServerBackResult();
            string[] resultArrary = qqServerBackResult.Split('&');
            List<string> listResult = new List<string>(resultArrary);
            if (!listResult.Contains("RES=0"))
            {
                backResult.IsSuc = false;
                backResult.Result = "请求服务器失败";
            }
            if (!listResult.Contains("RS=0"))
            {
                backResult.IsSuc = false;
                backResult.Result = listResult[listResult.Count - 1].Replace("RA=", "");
            }
            if (listResult.Contains("RS=0"))
            {
                backResult.IsSuc = true;
            }
            return backResult;
        }

        #endregion

        #region 设置一个返回三位数的随机数
        /// <summary>
        /// 设置一个返回三位数的随机数
        /// </summary>
        /// <returns></returns>
        private int GetThreeRandom()
        {
            var random = new Random();
            return random.Next(200, 999);
        }


        #endregion



    }


}
